    //
//  AccountViewController.m
//  FanTest
//
//  Created by Cheng on 12/6/28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AccountViewController.h"
#import "SelectBudget.h"
#import "SharingCircles.h"
#import "Crop.h"
#import "BK_Enviroment.h"
#import "AccountInfo.h"
#import "EditGroupUser.h"
#import "EditPartner.h"
#import "EditPlanner.h"
#import "ChangePassword.h"
#import "RequestAPIFunction.h"

#define UPLOADACCOUNTINFO_API @"http://54.235.166.43/BeSpokeAPI/UploadAccountInfo.php"

#define SCROLLVIEW_SHAREGROUP 435
#define SHARECIRCLEWIDTH 135
#define SHARECIRCLEHEIGHT 145
#define SHARECIRCLEWIDTH_SPACE 6
#define SHARECIRCLEWIDTH_LEFTBORDER 49
#define STARTING_SHARECIRCLE_TAG 201

#define LOADINGVIEW_TAG 10000000
#define LOADINGIMAGE_TAG 20000000

static int m_ProfileImageButton = 0;

static int m_X_axis_AddSharingCircle = 0;
static int m_Y_axis_AddSharingCircle = 0;
static int m_X_axis_SharingCircles = 0;
static int m_Y_axis_SharingCircles = 0;

@implementation AccountViewController
@synthesize BK_En_object;
@synthesize Lab_yourName, Lab_yourEmail, Lab_yourPartnerName, Lab_yourPartnerEmail, Lab_PlannerName, Lab_PlannerEmail;
@synthesize Ctr_poper;
@synthesize Bt_yourPhoto;
@synthesize Txt_yourName, Txt_yourPartnerName;
@synthesize TK_CalenderVW, BT_calendar, Lab_wedding;
@synthesize BT_editBudget, Vw_selectBudget, IVw_Background, ActivityView;
@synthesize Txt_Password, Txt_UserName,Array_AccountInfo, Bt_Channel;
@synthesize SVw_SharingCircles, Bt_AddSharingCircle, Bt_ChangePassword;
@synthesize Vw_AddSharingCircle, Vw_EditGroupUser, Vw_EditPartner, Vw_EditPlanner, Vw_ChangePassword, Bt_Continue, Vw_Channel;
@synthesize delegate;

-(IBAction)ReturnPressed:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(IBAction)OpenFeedSet:(id)sender {
    
    [self.delegate AccountViewControllerDelegateDidFeedSet:self];
}

-(IBAction)OpenMyBooks:(id)sender {
    
    [self.delegate AccountViewControllerDelegateDidMyBooks:self];
}

-(IBAction)ConitunePressed:(id)sender {
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    
    [self.delegate AccountViewControllerDelegateDidStream:self];
    
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
}

-(IBAction)showBudgetSection:(id)sender
{
    if(Vw_selectBudget == nil)
    {
        
        UIButton *BT_showBudget = (UIButton *)sender;
        
        [[NSBundle mainBundle] loadNibNamed:@"SelectBudget" owner:self options:nil];
        Vw_selectBudget.frame = CGRectMake(BT_showBudget.frame.origin.x, BT_showBudget.frame.origin.y -Vw_selectBudget.frame.size.width, Vw_selectBudget.frame.size.width, Vw_selectBudget.frame.size.height);
        Vw_selectBudget.delegate = self;
        
        [Vw_selectBudget initArray];
    }
    [Vw_selectBudget setAlpha:0];
    
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_selectBudget setAlpha:1.0];[self.view addSubview:Vw_selectBudget];}
                     completion:^(BOOL finished){ }];

}


-(IBAction)showCalendar:(id)sender
{
    TK_CalenderVW.frame = CGRectMake(BT_calendar.frame.origin.x + 40, BT_calendar.frame.origin.y -TK_CalenderVW.frame.size.height , TK_CalenderVW.frame.size.width  , TK_CalenderVW.frame.size.height);
    
    [UIView animateWithDuration:0.2
                     animations:^{[TK_CalenderVW setAlpha:1.0];}
                     completion:^(BOOL finished){[self.view addSubview:TK_CalenderVW]; }];

}

-(IBAction)showPhotoLib:(id)sender
{    
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    m_ProfileImageButton = ((UIButton *)sender).tag;
    
    Ctr_poper = [[UIPopoverController alloc] initWithContentViewController:picker];
    
    Ctr_poper.delegate = self;
    [Ctr_poper presentPopoverFromRect:((UIButton*)sender).frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    
    //[self presentModalViewController:picker animated:YES];
}

-(IBAction)EditPartner:(id)sender {
    
    if(Vw_EditPartner == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"EditPartner" owner:self options:nil];
        Vw_EditPartner.frame = CGRectMake(0, 0, Vw_EditPartner.frame.size.width, Vw_EditPartner.frame.size.height);
        [Vw_EditPartner.Bt_Cancel.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11]];
        [Vw_EditPartner.Bt_Done.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11]];
        
        Vw_EditPartner.Tf_PartnerName.text = BK_En_object.partnerName;
        Vw_EditPartner.Tf_PartnerEmail.text = BK_En_object.partnerEmail;
        Vw_EditPartner.delegate = self;
    }
    [Vw_EditPartner setAlpha:0];
    
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_EditPartner setAlpha:1.0];[self.view addSubview:Vw_EditPartner];}
                     completion:^(BOOL finished){ }];
}

-(IBAction)EditPlanner:(id)sender {
    
    if(Vw_EditPlanner == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"EditPlanner" owner:self options:nil];
        Vw_EditPlanner.frame = CGRectMake(0, 0, Vw_EditPlanner.frame.size.width, Vw_EditPlanner.frame.size.height);
        [Vw_EditPlanner.Bt_Cancel.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11]];
        [Vw_EditPlanner.Bt_Done.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11]];
        
        Vw_EditPlanner.Tf_PlannerName.text = BK_En_object.PlanerName;
        Vw_EditPlanner.Tf_PlannerEmail.text = BK_En_object.PlanerEmail;
        Vw_EditPlanner.delegate = self;
    }
    [Vw_EditPlanner setAlpha:0];
    
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_EditPlanner setAlpha:1.0];[self.view addSubview:Vw_EditPlanner];}
                     completion:^(BOOL finished){ }];
}

-(IBAction)ChangePassword:(id)sender {
    
    if(Vw_ChangePassword == nil)
    {
        [[NSBundle mainBundle] loadNibNamed:@"ChangePassword" owner:self options:nil];
        Vw_ChangePassword.frame = CGRectMake(0, 0, Vw_ChangePassword.frame.size.width, Vw_ChangePassword.frame.size.height);
        [Vw_ChangePassword.Bt_Cancel.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11]];
        [Vw_ChangePassword.Bt_Done.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11]];
        
        Vw_ChangePassword.delegate = self;
    }
    [Vw_ChangePassword setAlpha:0];
    
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_ChangePassword setAlpha:1.0];[self.view addSubview:Vw_ChangePassword];}
                     completion:^(BOOL finished){ }];
}

-(IBAction)showChannel:(id)sender
{
    if(Vw_Channel == nil) {
        
        Vw_Channel = [[[NSBundle mainBundle] loadNibNamed:@"ChannelView" owner:self options:nil] objectAtIndex:0];Vw_Channel.frame = CGRectMake(Bt_Channel.center.x-Vw_Channel.frame.size.width/2, Bt_Channel.frame.origin.y+Bt_Channel.frame.size.height, Vw_Channel.frame.size.width, Vw_Channel.frame.size.height);
        Vw_Channel.delegate = self;
        [self.Vw_Channel initArray];
        //[Vw_Channel initArray:((AccountInfo *)[self.Array_AccountInfo objectAtIndex:0]).Initialization.Array_Channels];
    }
    [Vw_Channel setAlpha:0];
    
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_Channel setAlpha:1.0];[self.view addSubview:Vw_Channel];}
                     completion:^(BOOL finished){ }];
    
}

-(IBAction)SignOutPressed:(id)sender {
    
    UIAlertView *AVw_Alert = [[UIAlertView alloc] initWithTitle:@"Are you sure?"
                                                        message:@"Do you really want to logout"
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Ok",nil];
    AVw_Alert.delegate = self;
    [AVw_Alert show];
}

-(void)GetSharingCircles {
    
    NSMutableArray *Array_Group =[[NSMutableArray alloc] initWithArray:((AccountShareGroup *)((AccountInfo *)[Array_AccountInfo  objectAtIndex:0]).ShareGroup).GroupArray];
    
    for (AccountGroup *Group in Array_Group) {
        
        //Set SharingCircles.
        SharingCircles *SC_Cell = [[[NSBundle mainBundle] loadNibNamed:@"SharingCircles" owner:self options:nil] objectAtIndex: 0];
        [SC_Cell setTag:[Array_Group indexOfObject:Group]+STARTING_SHARECIRCLE_TAG];
        [SC_Cell.Lab_GroupName setTag:[Array_Group indexOfObject:Group]+STARTING_SHARECIRCLE_TAG];
        [SC_Cell.Lab_UserCount setTag:[Array_Group indexOfObject:Group]+STARTING_SHARECIRCLE_TAG];
        [SC_Cell.Bt_Circles setTag:[Array_Group indexOfObject:Group]+STARTING_SHARECIRCLE_TAG];
        
        SC_Cell.Lab_GroupName.text = Group.NS_Name;
        SC_Cell.Lab_UserCount.text = [NSString stringWithFormat:@"%d", [Group.UserArray count]];
        [SC_Cell.Bt_Circles addTarget:self action:@selector(OpenGroupPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        
        //Calculate Circle X,Y in Scrollview.
        if(m_X_axis_SharingCircles+SHARECIRCLEWIDTH_LEFTBORDER>SCROLLVIEW_SHAREGROUP) {
            m_X_axis_SharingCircles = 0;
            m_Y_axis_SharingCircles = m_Y_axis_SharingCircles+SHARECIRCLEHEIGHT;
        }
        [SC_Cell setFrame:CGRectMake(m_X_axis_SharingCircles, m_Y_axis_SharingCircles, SC_Cell.frame.size.width,SC_Cell.frame.size.height)];
        m_X_axis_SharingCircles = m_X_axis_SharingCircles+SHARECIRCLEWIDTH+SHARECIRCLEWIDTH_LEFTBORDER;
        m_X_axis_AddSharingCircle = m_X_axis_SharingCircles;
        
        if(m_X_axis_AddSharingCircle+SHARECIRCLEWIDTH_LEFTBORDER>SCROLLVIEW_SHAREGROUP) {
            m_Y_axis_AddSharingCircle = m_Y_axis_SharingCircles+SHARECIRCLEHEIGHT;
            m_X_axis_AddSharingCircle = 0;
        }
    
        [SVw_SharingCircles addSubview:SC_Cell];
    }
    
    
    //Set Add_SharingCircle.
    [Vw_AddSharingCircle setFrame:CGRectMake(m_X_axis_AddSharingCircle, m_Y_axis_AddSharingCircle, Vw_AddSharingCircle.frame.size.width,Vw_AddSharingCircle.frame.size.height)];
    [Bt_AddSharingCircle addTarget:self action:@selector(OpenGroupPressed:) forControlEvents:UIControlEventTouchUpInside];
    [Bt_AddSharingCircle setTag:[Array_Group count]+STARTING_SHARECIRCLE_TAG];
    [Vw_AddSharingCircle setTag:[Array_Group count]+STARTING_SHARECIRCLE_TAG];
    
    [SVw_SharingCircles setContentSize:CGSizeMake(SVw_SharingCircles.frame.size.width, m_Y_axis_AddSharingCircle+Vw_AddSharingCircle.frame.size.height)];
}

- (void)OpenGroupPressed:(id)sender {
    
    if(Vw_EditGroupUser == nil) {
        
        [[NSBundle mainBundle] loadNibNamed:@"EditGroupUser" owner:self options:nil];
        Vw_EditGroupUser.frame = CGRectMake(0, 0, Vw_EditGroupUser.frame.size.width, Vw_EditGroupUser.frame.size.height);
        Vw_EditGroupUser.BK_En_object = self.BK_En_object;
        
        [Vw_EditGroupUser.Bt_DeleteMyGroup.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11]];
        [Vw_EditGroupUser.Bt_SaveMyGroup.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11]];
        [Vw_EditGroupUser.Bt_Cancel.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:11]];
        Vw_EditGroupUser.delegate = self;
        
        if ([sender tag]-STARTING_SHARECIRCLE_TAG==[((AccountShareGroup *)((AccountInfo *)[Array_AccountInfo objectAtIndex:0]).ShareGroup).GroupArray count]) {
            Vw_EditGroupUser.Bt_DeleteMyGroup.hidden = YES;
            [Vw_EditGroupUser EditGroupUser:nil];
        }
        else
            [Vw_EditGroupUser EditGroupUser:(AccountGroup *)[((AccountShareGroup *)((AccountInfo *)[Array_AccountInfo objectAtIndex:0]).ShareGroup).GroupArray objectAtIndex:[sender tag]-STARTING_SHARECIRCLE_TAG]];
    }
    [Vw_EditGroupUser setAlpha:0];
    [UIView animateWithDuration:0.5
                     animations:^{[Vw_EditGroupUser setAlpha:1.0];[self.view addSubview:Vw_EditGroupUser];}
                     completion:^(BOOL finished){ }];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ((self.BK_En_object.passWord!=nil)&&(![self.BK_En_object.passWord isEqualToString:@""])&&
        ([((AccountInfo *)[Array_AccountInfo objectAtIndex:0]).NS_Organization isEqualToString:@"TPN"]))
        Bt_ChangePassword.hidden = NO;
    
    [Bt_Continue.titleLabel setFont:[UIFont fontWithName:@"EamesCenturyModern-Regular" size:12]];
    
    m_X_axis_AddSharingCircle = 0;
    m_Y_axis_AddSharingCircle = 0;
    m_X_axis_SharingCircles = 0;
    m_Y_axis_SharingCircles = 0;
    
    // Temperary Testing only  *************
    //BK_En_object = [[BK_Enviroment alloc] init];
    
    // *******************
    
    TK_CalenderVW = [[TKCalendarMonthView alloc] init];
    TK_CalenderVW.delegate = self;
    TK_CalenderVW.dataSource = self;
    
    
    Lab_yourName.text = [BK_En_object.fullName uppercaseString];
    Lab_yourEmail.text = BK_En_object.userName;
    Lab_yourPartnerName.text = [BK_En_object.partnerName uppercaseString];
    Lab_yourPartnerEmail.text = BK_En_object.partnerEmail;
    Lab_PlannerName.text = [BK_En_object.PlanerName uppercaseString];
    Lab_PlannerEmail.text = BK_En_object.PlanerEmail;
    Lab_wedding.text = BK_En_object.NS_Calendar;
    
    if(BK_En_object.NS_Budget!=nil)
        [self.BT_editBudget setTitle:BK_En_object.NS_Budget forState:UIControlStateNormal];
    
    if(BK_En_object.NS_Channel!=nil)
        [Bt_Channel setTitle:BK_En_object.NS_Channel forState:UIControlStateNormal];
    
    if(BK_En_object.yourPhoto!=nil)
    {
        [self.Bt_yourPhoto setImage:[BK_En_object.yourPhoto imageByScalingAndCroppingForSize:CGSizeMake(self.Bt_yourPhoto.frame.size.width, self.Bt_yourPhoto.frame.size.height)] forState:UIControlStateNormal];
    }
    
    [self GetSharingCircles];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark - Interface Orientation Management

//For iOS 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

//For iOS 6
-(BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

#pragma mark - UIImagePickerControllerDelegate

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [self.Ctr_poper dismissPopoverAnimated:true];
    
    
    /**float ratio = 0;
    CGSize BtSize;
    
    if(image.size.height < image.size.width)
    {
        ratio = image.size.width / 105;
    }
    else {
        ratio = image.size.height / 105;
    }
    
    BtSize = CGSizeMake(image.size.width / ratio, image.size.height / ratio);**/
    
    UIImage *resizedImg = [image imageByScalingAndCroppingForSize:CGSizeMake(self.Bt_yourPhoto.frame.size.width, self.Bt_yourPhoto.frame.size.height)];
    
    // NSLog(@"pickerTitle %d" , m_ProfileImageButton);
    if(m_ProfileImageButton == 101)
    {
        [Bt_yourPhoto setImage:resizedImg forState:UIControlStateNormal];
        [BK_En_object setAccountYourImage:resizedImg];
    }
    
}

#pragma mark -
#pragma mark TKCalendarMonthViewDelegate methods

- (void)calendarMonthView:(TKCalendarMonthView *)monthView didSelectDate:(NSDate *)d {
	NSLog(@"calendarMonthView didSelectDate");
    
    
	NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
	[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
	[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
	NSString *dateString = [dateFormatter stringFromDate:[monthView dateSelected]];
	
    [self.BK_En_object setAccountYourCalendar:dateString];
	Lab_wedding.text = dateString;
    
    [UIView animateWithDuration:0.2
                     animations:^{[TK_CalenderVW setAlpha:0.0];}
                     completion:^(BOOL finished){ [TK_CalenderVW removeFromSuperview]; }];
}

- (void)calendarMonthView:(TKCalendarMonthView *)monthView monthDidChange:(NSDate *)d {
	NSLog(@"calendarMonthView monthDidChange");	
}

#pragma mark -
#pragma mark TKCalendarMonthViewDataSource methods

- (NSArray*)calendarMonthView:(TKCalendarMonthView *)monthView marksFromDate:(NSDate *)startDate toDate:(NSDate *)lastDate {	
	NSLog(@"calendarMonthView marksFromDate toDate");	
	NSLog(@"Make sure to update 'data' variable to pull from CoreData, website, User Defaults, or some other source.");
    NSArray *data = [NSArray arrayWithObjects:NULL];
    
    return data;
}

#pragma mark -
#pragma mark SelectBudgetDelegate

-(void) BudgetDidSelected:(NSString *)Txt_budget
{
    [BK_En_object setAccountYourBudget:Txt_budget];
    [BT_editBudget setTitle:Txt_budget forState:UIControlStateNormal];
    [BT_editBudget setContentEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    BT_editBudget.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

}

#pragma mark -
#pragma mark EditGroupUserDelegate

-(void)MoveShareCircles:(AccountGroup *)AG_SelectedGroup Action:(NSString *)CheckStatus {
    
    
    [Vw_EditGroupUser removeFromSuperview];
    
    Vw_EditGroupUser = nil;
    
    int index = [((AccountShareGroup *)((AccountInfo *)[Array_AccountInfo objectAtIndex:0]).ShareGroup).GroupArray count];
    int Selected_index = [((AccountShareGroup *)((AccountInfo *)[Array_AccountInfo objectAtIndex:0]).ShareGroup).GroupArray indexOfObject:AG_SelectedGroup]+STARTING_SHARECIRCLE_TAG;
    
    if ([CheckStatus isEqualToString:@"Create"]) {
        
        m_X_axis_AddSharingCircle = Vw_AddSharingCircle.frame.origin.x;
        m_Y_axis_AddSharingCircle = Vw_AddSharingCircle.frame.origin.y;
        
        [((AccountShareGroup *)((AccountInfo *)[Array_AccountInfo objectAtIndex:0]).ShareGroup).GroupArray addObject:AG_SelectedGroup];
        
        //Set SharingCircles.
        SharingCircles *SC_Cell = [[[NSBundle mainBundle] loadNibNamed:@"SharingCircles" owner:self options:nil] objectAtIndex: 0];
        [SC_Cell setTag:index+STARTING_SHARECIRCLE_TAG];
        [SC_Cell.Lab_GroupName setTag:index+STARTING_SHARECIRCLE_TAG];
        [SC_Cell.Lab_UserCount setTag:index+STARTING_SHARECIRCLE_TAG];
        [SC_Cell.Bt_Circles setTag:index+STARTING_SHARECIRCLE_TAG];
        
        SC_Cell.Lab_GroupName.text = AG_SelectedGroup.NS_Name;
        SC_Cell.Lab_UserCount.text = [NSString stringWithFormat:@"%d", [AG_SelectedGroup.UserArray count]];
        
        SC_Cell.frame = CGRectMake(Vw_AddSharingCircle.frame.origin.x,
                                   Vw_AddSharingCircle.frame.origin.y,
                                   SC_Cell.frame.size.width,
                                   SC_Cell.frame.size.height);
        
        
        [SC_Cell.Bt_Circles addTarget:self action:@selector(OpenGroupPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        [UIView animateWithDuration:0.5
                         animations:^{[SVw_SharingCircles addSubview:SC_Cell];}
                         completion:^(BOOL finished){ }];
        
        //Calculate m_Y_axis_AddShareCircle.
        m_X_axis_AddSharingCircle = m_X_axis_AddSharingCircle + SHARECIRCLEWIDTH+SHARECIRCLEWIDTH_LEFTBORDER;
        if (m_X_axis_AddSharingCircle+SHARECIRCLEWIDTH_LEFTBORDER>SCROLLVIEW_SHAREGROUP) {
            m_Y_axis_AddSharingCircle = m_Y_axis_AddSharingCircle + SHARECIRCLEHEIGHT;
            m_X_axis_AddSharingCircle = 0;
        }
        
        //Animation.
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [Vw_AddSharingCircle setFrame:CGRectMake(m_X_axis_AddSharingCircle,
                                         m_Y_axis_AddSharingCircle,
                                         Vw_AddSharingCircle.frame.size.width,
                                         Vw_AddSharingCircle.frame.size.height)];
        [UIView commitAnimations];
        [Bt_AddSharingCircle setTag:index+STARTING_SHARECIRCLE_TAG+1];
        [Vw_AddSharingCircle setTag:index+STARTING_SHARECIRCLE_TAG+1];
        
        SVw_SharingCircles.contentSize = CGSizeMake(SVw_SharingCircles.frame.size.width, m_Y_axis_AddSharingCircle+Vw_AddSharingCircle.frame.size.height);
    }
    
    else if ([CheckStatus isEqualToString:@"Delete"]) {
        
        m_X_axis_SharingCircles = [self.view viewWithTag:Selected_index].frame.origin.x;
        m_Y_axis_SharingCircles = [self.view viewWithTag:Selected_index].frame.origin.y;
        m_X_axis_AddSharingCircle = [self.view viewWithTag:Selected_index].frame.origin.x;
        m_Y_axis_AddSharingCircle = [self.view viewWithTag:Selected_index].frame.origin.y;
        
        [((AccountShareGroup *)((AccountInfo *)[Array_AccountInfo objectAtIndex:0]).ShareGroup).GroupArray removeObject:AG_SelectedGroup];
        
        
        for (int i=Selected_index+1; i<index+STARTING_SHARECIRCLE_TAG; i++) {
            
            
            //Calculate Circle X,Y in Scrollview.
            if(m_X_axis_SharingCircles+SHARECIRCLEWIDTH_LEFTBORDER>SCROLLVIEW_SHAREGROUP) {
                m_X_axis_SharingCircles = 0;
                m_Y_axis_SharingCircles = m_Y_axis_SharingCircles+SHARECIRCLEHEIGHT;
            }
            
            //Animation.
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:1.0];
            [UIView setAnimationBeginsFromCurrentState:YES];
            ((SharingCircles *)[self.view viewWithTag:i]).frame = CGRectMake(m_X_axis_SharingCircles,m_Y_axis_SharingCircles, SHARECIRCLEWIDTH, SHARECIRCLEHEIGHT);
            [UIView commitAnimations];
            
            m_X_axis_SharingCircles = m_X_axis_SharingCircles+SHARECIRCLEWIDTH+SHARECIRCLEWIDTH_LEFTBORDER;
            m_X_axis_AddSharingCircle = m_X_axis_SharingCircles;
            if(m_X_axis_AddSharingCircle+SHARECIRCLEWIDTH_LEFTBORDER>SCROLLVIEW_SHAREGROUP) {
                m_Y_axis_AddSharingCircle = m_Y_axis_SharingCircles+SHARECIRCLEHEIGHT;
                m_X_axis_AddSharingCircle = 0;
            }
            
            //Replace previous cell tag.
            [((SharingCircles *)[self.view viewWithTag:i]).Lab_GroupName setTag:i-1];
            [((SharingCircles *)[self.view viewWithTag:i]).Lab_UserCount setTag:i-1];
            [((SharingCircles *)[self.view viewWithTag:i]).Bt_Circles setTag:i-1];
            [((SharingCircles *)[self.view viewWithTag:i]) setTag:i-1];
        }
        
        if (m_X_axis_AddSharingCircle>SCROLLVIEW_SHAREGROUP) {
            m_X_axis_AddSharingCircle = 0;
            m_Y_axis_AddSharingCircle = m_Y_axis_SharingCircles+SHARECIRCLEHEIGHT;
        }
        
        //Animation.
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:1.0];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [Vw_AddSharingCircle setFrame:CGRectMake(m_X_axis_AddSharingCircle,
                                         m_Y_axis_AddSharingCircle,
                                         Vw_AddSharingCircle.frame.size.width,
                                         Vw_AddSharingCircle.frame.size.height)];
        [((SharingCircles *)[self.view viewWithTag:Selected_index]).Lab_GroupName removeFromSuperview];
        [((SharingCircles *)[self.view viewWithTag:Selected_index]).Lab_UserCount removeFromSuperview];
        [((SharingCircles *)[self.view viewWithTag:Selected_index]).Bt_Circles removeFromSuperview];
        [(SharingCircles *)[self.view viewWithTag:Selected_index] removeFromSuperview];
        [UIView commitAnimations];
        
        //AddShareCircle modify tag.
        [Bt_AddSharingCircle setTag:index+STARTING_SHARECIRCLE_TAG-1];
        [Vw_AddSharingCircle setTag:index+STARTING_SHARECIRCLE_TAG-1];
        
        SVw_SharingCircles.contentSize = CGSizeMake(SVw_SharingCircles.frame.size.width, m_Y_axis_AddSharingCircle+Vw_AddSharingCircle.frame.size.height);
    }
    else if ([CheckStatus isEqualToString:@"Modify"]) {
        ((SharingCircles *)[self.view viewWithTag:Selected_index]).Lab_GroupName.text = AG_SelectedGroup.NS_Name;
        ((SharingCircles *)[self.view viewWithTag:Selected_index]).Lab_UserCount.text = [NSString stringWithFormat:@"%d", [AG_SelectedGroup.UserArray count]];
    }
}

#pragma mark - EditPartnerDelegate

- (void)CancelPressed_EditPartner {
    [Vw_EditPartner removeFromSuperview];
}

- (void)SavePartnerInfo:(NSString *)PartnerName SetPartnerEmail:(NSString *)PartnerEmail {
    [BK_En_object SetPartner:PartnerName SetEmail:PartnerEmail];
    Lab_yourPartnerName.text = [PartnerName uppercaseString];
    Lab_yourPartnerEmail.text = PartnerEmail;
    [Vw_EditPartner removeFromSuperview];
}

#pragma mark - EditPlannerDelegate

- (void)CancelPressed_EditPlanner {
    [Vw_EditPlanner removeFromSuperview];
}

- (void)SavePlannerInfo:(NSString *)PlannerName SetPlannerEmail:(NSString *)PlannerEmail {
    [BK_En_object setPlanerInfo:PlannerName Email:PlannerEmail];
    Lab_PlannerName.text = [PlannerName uppercaseString];
    Lab_PlannerEmail.text = PlannerEmail;
    [Vw_EditPlanner removeFromSuperview];
}

#pragma mark - ChangePasswordDelegate

- (void)CancelPressed_ChangePassword {
    
    Vw_ChangePassword.Tf_NewPassword.text = nil;
    Vw_ChangePassword.Tf_ConfirmPassword.text = nil;
    [Vw_ChangePassword removeFromSuperview];
}

#pragma mark - ChannelViewDelegate

- (void)uiViewTouched_Channel:(BOOL)wasInSide {
    if(!wasInSide) {
        [Vw_Channel removeFromSuperview];
    }
}

- (void)ChannelDidSelected:(NSString *)Channel {
    [BK_En_object setAccountChannel:Channel];
    [Bt_Channel setTitle:Channel forState:UIControlStateNormal];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex==1) {
        NSString *XML_Facebook = @"";
        NSString *XML_Twitter = @"";
        NSString *XML_Tumblr = @"";
        NSString *XML_Instagram = @"";
        
        [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
        
        if (BK_En_object.NS_FaceBookID!=nil)
            XML_Facebook = [NSString stringWithFormat:@"<Facebook ID=\"%@\" Token=\"%@\" Switch=\"%d\" />",
                            BK_En_object.NS_FaceBookID,
                            BK_En_object.NS_FaceBookToken,
                            BK_En_object.FacebookFeedCheck];
        
        if (BK_En_object.NS_OauthKey!=nil)
            XML_Twitter = [NSString stringWithFormat:@"<Twitter Key=\"%@\" Secret=\"%@\" Switch=\"%d\" />",
                           BK_En_object.NS_OauthKey,
                           BK_En_object.NS_OauthSecret,
                           BK_En_object.TwitterFeedCheck];
        
        if (BK_En_object.NS_Tumblr!=nil)
            XML_Tumblr = [NSString stringWithFormat:@"<Tumblr Name=\"%@\" Switch=\"%d\" />",
                          BK_En_object.NS_Tumblr,
                          BK_En_object.TumblrFeedCheck];
        
        if (BK_En_object.NS_InstagramToken!=nil)
            XML_Instagram = [NSString stringWithFormat:@"<Instagram Token=\"%@\" Switch=\"%d\" />",
                             BK_En_object.NS_InstagramToken,
                             BK_En_object.InstagramFeedCheck];
        
        
        NSMutableString *XML_GoogleReader = [NSMutableString string];
        
        for (NSString *NS_GoogleTitle in self.BK_En_object.Array_GoogleTitleSelected) {
            int index = [self.BK_En_object.Array_GoogleTitleSelected indexOfObject:NS_GoogleTitle];
            
            NSString *NS_GoogleContent = [self.BK_En_object.Array_GoogleContentSelected objectAtIndex:index];
            NSString *NS_GoogleLink = [self.BK_En_object.Array_GoogleReaderLink objectAtIndex:index];
            int GoogleSwitch = [[self.BK_En_object.Array_GoogleSwitchCheck objectAtIndex:index] intValue];
            
            [XML_GoogleReader appendString:[NSString stringWithFormat:@"<GoogleFeed Title=\"%@\" Content=\"%@\" Link=\"%@\" Switch=\"%d\" />",
                                            NS_GoogleTitle,
                                            NS_GoogleContent,
                                            NS_GoogleLink,
                                            GoogleSwitch]];
        }
        
        if (BK_En_object.NS_Calendar==nil)
            BK_En_object.NS_Calendar = @"";
        
        if (BK_En_object.NS_Channel==nil)
            BK_En_object.NS_Channel = @"";
        
        NSData *responseData =
        [RequestAPIFunction XML_PostRequest:[NSString stringWithFormat:@"<Account UserName=\"%@\" Channel=\"%@\" WeddingDate=\"%@\">%@%@%@%@%@</Account>",
                                             BK_En_object.userName,
                                             BK_En_object.NS_Channel,
                                             BK_En_object.NS_Calendar,
                                             XML_Facebook,
                                             XML_Twitter,
                                             XML_Tumblr,
                                             XML_Instagram,
                                             [XML_GoogleReader stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"]]
                                  urlString:UPLOADACCOUNTINFO_API];
        
        //No Network Connection or time out.
        if (responseData == nil)
            return;
        
        [BK_En_object clearEnviroment];
        
        //Clear Instagram cookie.
        NSString *kDialogBaseURL = @"https://instagram.com/";
        NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        NSArray* instagramCookies = [cookies cookiesForURL:[NSURL URLWithString:kDialogBaseURL]];
        
        for (NSHTTPCookie* cookie in instagramCookies)
            [cookies deleteCookie:cookie];
        
        [self.delegate AccountViewControllerDelegateDidLogoScreen:self];
        
        [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
    }
}

#pragma mark - UIActivityIndicatorView

-(void)threadStartAnimating {
    
    [IVw_Background setBackgroundColor:[UIColor blackColor]];
    [IVw_Background setAlpha:0.5];
    
    ActivityView.hidden = NO;
    ActivityView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2);
    
    [self.view bringSubviewToFront:self.IVw_Background];
    
    [self.view bringSubviewToFront:self.ActivityView];
    
    [ActivityView startAnimating];
}

-(void)threadStopAnimating {
    
    [(UIActivityIndicatorView *)self.view viewWithTag:LOADINGIMAGE_TAG].hidden = YES;
    [(UIActivityIndicatorView *)[self.view viewWithTag:LOADINGIMAGE_TAG] stopAnimating];
    
    [[NSOperationQueue mainQueue] addOperationWithBlock:
     ^(void) {
         [[self.view viewWithTag:LOADINGVIEW_TAG] removeFromSuperview];
         [[self.view viewWithTag:LOADINGIMAGE_TAG] removeFromSuperview];
     }];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

#pragma mark - Touch

-(void) uiViewTouched:(BOOL)wasInSide
{
    if(!wasInSide)
    {
        [Vw_selectBudget removeFromSuperview];
        [Vw_EditGroupUser removeFromSuperview];
        
        Vw_EditGroupUser = nil;
    }
}

@end
